package com.example.sprinbootdemo.controller;

import com.example.sprinbootdemo.entity.ApiResponse;
import com.example.sprinbootdemo.entity.CarModel;
import com.example.sprinbootdemo.service.CarModelService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/carmodels")
public class CarModelController {

    private static final Logger logger = LoggerFactory.getLogger(CarModelController.class);

    @Autowired
    private CarModelService carModelService;

    // 获取所有的车型记录
    @GetMapping("/getList")
    public ResponseEntity<ApiResponse> getList() {
        List<CarModel> list;
        try {
            list = carModelService.getAllCarModels();
            ApiResponse response = new ApiResponse(1, "成功", list);
            return ResponseEntity.ok(response);
        } catch (Exception e) {
            logger.error("获取车型记录列表失败", e);
            ApiResponse response = new ApiResponse(0, "获取车型记录列表失败: " + e.getMessage(), null);
            return ResponseEntity.status(500).body(response);
        }
    }

    // 根据 ModelID 获取车型记录
    @GetMapping("/get/{modelId}")
    public ResponseEntity<ApiResponse> getCarModelByModelId(@PathVariable int modelId) {
        try {
            CarModel carModel = carModelService.getCarModelByModelId(modelId);
            if (carModel == null) {
                ApiResponse response = new ApiResponse(0, "数据不存在", null);
                return ResponseEntity.status(404).body(response);
            }
            ApiResponse response = new ApiResponse(1, "成功", carModel);
            return ResponseEntity.ok(response);
        } catch (Exception e) {
            logger.error("获取数据失败, ModelID: {}", modelId, e);
            ApiResponse response = new ApiResponse(0, "获取数据失败: " + e.getMessage(), null);
            return ResponseEntity.status(500).body(response);
        }
    }

    // 添加新的车型记录
    @PostMapping("/addDate")
    public ResponseEntity<ApiResponse> addCarModel(@RequestBody CarModel carModel) {
        try {
            logger.info("添加车型记录: {}", carModel);
            carModelService.addCarModel(carModel);
            ApiResponse response = new ApiResponse(1, "车型记录添加成功", null);
            return ResponseEntity.status(201).body(response); // 返回 201 表示创建成功
        } catch (Exception e) {
            logger.error("添加车型记录失败", e);
            ApiResponse response = new ApiResponse(0, "添加车型记录失败: " + e.getMessage(), null);
            return ResponseEntity.status(500).body(response);
        }
    }

    // 删除指定 ID 的车型记录
    @DeleteMapping("/delete/{modelId}")
    public ResponseEntity<ApiResponse> deleteCarModelByModelId(@PathVariable int modelId) {
        try {
            logger.info("删除车型记录, ModelID: {}", modelId);
            carModelService.deleteCarModelByModelId(modelId);
            ApiResponse response = new ApiResponse(1, "车型记录删除成功", null);
            return ResponseEntity.ok(response);
        } catch (Exception e) {
            logger.error("删除车型记录失败, ModelID: {}", modelId, e);
            ApiResponse response = new ApiResponse(0, "删除车型记录失败: " + e.getMessage(), null);
            return ResponseEntity.status(500).body(response);
        }
    }

    // 更新指定 ID 的车型记录
    @PutMapping("/update")
    public ResponseEntity<ApiResponse> updateCarModelByModelId(@RequestBody CarModel carModel) {
        try {
            logger.info("更新车型记录: {}", carModel);
            carModelService.updateCarModelByModelId(carModel);
            ApiResponse response = new ApiResponse(1, "车型记录更新成功", null);
            return ResponseEntity.ok(response);
        } catch (Exception e) {
            logger.error("更新车型记录失败", e);
            ApiResponse response = new ApiResponse(0, "更新车型记录失败: " + e.getMessage(), null);
            return ResponseEntity.status(500).body(response);
        }
    }
}